<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/4/20
 * Time: 10:50
 */

namespace Admin\Controller;


use Think\Controller;

class CbfxController extends AdminController
{
    public function _myinitialize()
    {
        $this->db = M('khzy');
        $this->db1 = M('dkzy');
        $this->db2 = M('cszy');
        $this->db3 = M('fkcb');
        $this->db4 = M('ftth');
    }

    //http://www.glxt.com/admin/cbfx/lst
    public function lst()
    {
        $where['status'] = array('gt', 0);

        //注意这里默认显示所有月份的数据
        $month = I('month');
        $year = I('year');
        if (!empty($month) && !empty($year)) {
            $where['yf'] = $year . $month;
        }

        //求出所有月份khzy表中ys的总和，即业务收入
        $ywsr = $this->db->where($where)->group('yf')->order('yf desc')->getField('yf,sum(ys)');
        $lsts = array();
        foreach ($ywsr as $k => $v) {
            $lsts[$k]['ywsr'] = $v;
        }
        foreach ($lsts as $k => $v) {
            $ywsr = $lsts[$k]['ywsr'];
            $map['yf'] = $k;
            $map['status']=array('gt',0);
            //求出当前月份下所有的dk_ids为$dk_ids_str
            $dk_ids_arr = $this->db->where($map)->getField('dkzy_ids', true);
            $dk_ids_arr = array_values(array_unique($dk_ids_arr));
            $dk_ids_str = implode(',', $dk_ids_arr);
            $dk_ids_arr = explode(',', $dk_ids_str);
            $dk_ids_arr = array_values(array_unique($dk_ids_arr));
            $dk_ids_str = implode(',', $dk_ids_arr);
            //根据$dk_ids_str求出当前月份下所有的dkzy中sjcb之和
            $dk_where['status']=array('gt',0);
            $dk_where['id'] = array('in', $dk_ids_str);

            $dkcb = $this->db1->where($dk_where)->getField('sum(sjcb)');
            $lsts[$k]['dkcb'] = $dkcb;//带宽成本
            $lsts[$k]['dk_zb'] = round($dkcb / $ywsr * 100, 2) . "%";//带宽占比


            //求出当前月份下所有的cs_ids为$cs_ids_str
            $cs_ids_arr = $this->db->where($map)->getField('cszy_ids', true);
            $cs_ids_arr = array_values(array_unique($cs_ids_arr));
            $cs_ids_str = implode(',', $cs_ids_arr);
            $cs_ids_arr = explode(',', $cs_ids_str);
            $cs_ids_arr = array_values(array_unique($cs_ids_arr));
            $cs_ids_str = implode(',', $cs_ids_arr);
            //根据$dk_ids_str求出当前月份下所有的cszy中ys之和
            $cs_where['id'] = array('in', $cs_ids_str);
            $cs_where['status']=array('gt',0);

            $cscb = $this->db2->where($cs_where)->getField('sum(ys)');
            $lsts[$k]['cscb'] = $cscb;
            $lsts[$k]['cs_zb'] = round($cscb / $ywsr * 100, 2) . "%";

            //求出当前月份下所有返款成本的总和
            $fk_where['yf'] = $k;
            $fk_where['status']=array('gt',0);

            $fkcb = $this->db3->where($fk_where)->getField('sum(ys)');
            $lsts[$k]['fkcb'] = $fkcb;
            $lsts[$k]['fk_zb'] = round($fkcb / $ywsr * 100, 2) . "%";//返款占比

            //求出当前月份下所有ftth成本的总和
            $ftth_where['yf'] = $k;
            $ftth_where['status']=array('gt',0);

            $ftthcb = $this->db4->where($ftth_where)->getField('sum(xj)');
            $lsts[$k]['ftthcb'] = $ftthcb;
            $lsts[$k]['ftthcb_zb'] = round($ftthcb / $ywsr * 100, 2) . "%";

            //成本总和
            $cbxj = $lsts[$k]['cbxj'] = $dkcb + $cscb + $fkcb + $ftthcb;

            //毛利额
            $mle = $lsts[$k]['mle'] = $ywsr - $cbxj;

            //毛利率
            $lsts[$k]['mll'] = round($mle / $ywsr * 100, 2) . "%";

        }

        $this->assign('lsts', $lsts);
        $this->display();
    }
}